TreeGen = class(WorldGenerator)

function TreeGen:init(world,rand)
    WorldGenerator.init(self,world,rand)
    self.treesMinHeight = 5
end

function TreeGen:generate(x,y,z)
    while self:isAirBlock(x,y,z) do
        y = y - 1
    end
    local id = self:getBlock(x,y,z)
    if id == Block.soil.id or id == Block.grass.id then
        self:genTreeAt(x,y + 1,z)
        return
    end
end

function TreeGen:genTreeAt(x,y,z)
    local h = self.rand:rndInt(3,5)
    for xa = -2,2 do
        for za = -2,2 do
            self:setBlock(x + xa,y + h,z + za,Block.leaf.id)
            self:setBlock(x + xa,y + h + 1,z + za,Block.leaf.id)
        end
    end
    for xa = -1,1 do
        for za = -1,1 do
            self:setBlock(x + xa,y + h + 2,z + za,Block.leaf.id)
        end
    end
    self:setBlock(x,y + h + 3,z,Block.leaf.id)
    for ya = 0,h do
        self:setBlock(x,y + ya,z,Block.trunk.id)
    end
end
